package com.javaPro.myProject.modules.homework.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.common.util.uploadUtil;
import com.javaPro.myProject.modules.files.entity.TFile;
import com.javaPro.myProject.modules.homework.entity.Homework;
import com.javaPro.myProject.modules.homework.service.HomeworkService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;

/**
 * 到访体验表(Homework)表控制层
 *
 */
@RestController
@RequestMapping("homework")
public class HomeworkController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private HomeworkService homeworkService;
    @Resource
    private uploadUtil uploadUtil;

    /**
     * 分页查询
     *
     * @param homework 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public ListByPage queryByPage(Homework homework) {
        startPage();
        return getList(this.homeworkService.queryByPage(homework));
    }
    @GetMapping("listT")
    public ListByPage queryByPageT(Homework homework) {
        startPage();
        return getList(this.homeworkService.queryByPageT(homework));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("detail")
    public AjaxResult queryById(Integer id) {
        return AjaxResult.success(this.homeworkService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param homework 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(Homework homework, MultipartFile file) {
        if (file!= null){
            String upload = uploadUtil.upload(file);
            homework.setHomeworkFile(upload);
        }
        return AjaxResult.success(this.homeworkService.insert(homework));
    }

    /**
     * 编辑数据
     *
     * @param homework 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public AjaxResult edit(Homework homework , MultipartFile file)  {
        if (file!= null){
            String upload = uploadUtil.upload(file);
            homework.setHomeworkFile(upload);
        }
        return AjaxResult.success(this.homeworkService.update(homework));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("del")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.homeworkService.deleteById(id));
    }
    /**
     * 文件下载

     */
    @GetMapping("/download")
    @ResponseBody
    public void download(HttpServletResponse response, Integer id){
        Homework homework = homeworkService.queryById(id);
        String filePath = homework.getHomeworkFile();
        try {
            uploadUtil.downloadLocal(response,filePath,homework.getHomeworkTitle());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

